/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class ShopScene extends Scene {

    private static final int LIST_AVATAR = 0;
    private static final int ITEM_SHOP = 1;
    private static final int ITEM_BAG = 2;
    private static final int NUMBER_ICON_PER_PAGE = 2;
    private static final int DISTANCE_BETWEEN_DOT = 5;
    private int numberItemsShow;
    private int itemShopStartIndex;
    private int itemBuyStartIndex;
    private int itemAvaStartIndex;
    private int itemSelectedIndex;
    private int numberAvatars;
//    private int pageIndex;
    private int myMoney;
    private int numberItems;
    private int showType;
    private int numberPages;
    private int currentPageIndex;
    private int dotImageWidth;
    private int dotPaddingLeft;
    private int itemWidth;
    private int itemHeight;
    private int myGender;
    private Image bottomBarImage;
    private Image topBarImage;
    private Image shopItems;
    private Image rowTransparentBackground;
    private Image dotImages;
    private Image rowTransparentSelected;
    private Image avatarImage;
    private String[] commentString = {"Tăng 40% may mắn trong 24h",
        "Tăng 20% may mắn trong 24h",
        "Tăng 10% may mắn trong 24h",
        "Để thực hiện cầu hôn và kết hôn",
        "Thông báo trên kênh thế giới"};

    public ShopScene(MainCanvas parent) {
        super(parent);
        initValue();
        initObject();
    }

    private void initValue() {
        numberItemsShow = 2;
        myMoney = gameData.getMyMoney();
        numberItems = 5;
        numberAvatars = gameData.getNumberItems() - numberItems;
        Debug.d("number avatar :" + numberAvatars);
        myGender = gameData.getMyGender();

        if (Debug.isTest) {
//            numberItemActives = 10;
//            listItemActives = new ItemActive[numberItemActives];
//            for (int i = 0; i < numberItemActives; i++) {
//                listItemActives[i] = new ItemActive(i % 4, 1000);
//            }
            numberAvatars = 9;
        }
        setUpScene(0);
    }

    private void initObject() {
        try {
            bottomBarImage = library.getBottomBarImage();
            topBarImage = library.getTopBarImage();
            shopItems = Utils.loadImage("shop_items.png");
            dotImages = Utils.loadImage("dot.png");
            avatarImage = Utils.loadImage("new_avatar.png");
            dotImageWidth = dotImages.getWidth() >> 1;
            rowTransparentSelected = Utils.createTransparentImage(0xff000000, canvasWidth, shopItems.getHeight() + 13);
            rowTransparentBackground = Utils.createTransparentImage(Constants.OPACITY_BLACK, canvasWidth, shopItems.getHeight() + 13);
            itemWidth = shopItems.getWidth() / 6;
            itemHeight = shopItems.getHeight();
            if (screenMode == SCREEN_PORTRAIT) {
                dotPaddingLeft = (canvasWidth - (numberPages - 1) * (DISTANCE_BETWEEN_DOT + dotImageWidth)) >> 1;
            } else {
                dotPaddingLeft = canvasWidth / 2 - 50;
            }
        } catch (IOException ex) {
        }
    }

    public void setListSubMenu() {
        subMenu.setList(new String[]{"Avatar", "Cửa hàng", "Túi đồ"});
    }

    public void doLoop() {
    }
//------------------------------------------------------------------------------

    public void paintComponents(Graphics g) {
        paintBarMenu(g);
        if (showType == ITEM_BAG) {
            if (gameData.numberItemBuy > 0) {
                paintBagItem(g);
            } else {
                PiPoDesigner.drawString(g, "Bạn không có item nào trong túi đồ", PiPoDesigner.COLOR_BLACK,
                        PiPoDesigner.COLOR_WHITE, leftScreen + 20, topScreen + 50);
            }
        } else if (showType == ITEM_SHOP) {
            paintShopItem(g);
        } else {
            paintAvatar(g);
        }
        paintPageMark(g);
    }

    private void paintAvatar(Graphics g) {
        int paddingTop = 7;
        int paddingLeft = 8;
        int topShopScreen = topBarImage.getHeight() + 5;
        if (screenMode == SCREEN_PORTRAIT) {
            PiPoDesigner.drawString(g, "Avatar", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 5, topShopScreen);
            topShopScreen += 30;
        }

        fillborder(g, leftScreen, topShopScreen, canvasWidth, 1, 0xedc900);
        for (int i = itemAvaStartIndex; i < numberItemsShow + itemAvaStartIndex; i++) {
            int leftShopScreen = 0;
            topShopScreen += 1;
            if (i == itemSelectedIndex) {
                g.drawImage(rowTransparentSelected, leftScreen, topShopScreen, 0);
            } else {
                g.drawImage(rowTransparentBackground, leftScreen, topShopScreen, 0);
            }
            topShopScreen += paddingTop;
            leftShopScreen += paddingLeft;
            // g.drawImage(shopItems[i + itemShopStartIndex], leftShopScreen, topShopScreen, 0);
            Utils.drawFrame(g, avatarImage, leftShopScreen, topShopScreen, i + 1, myGender,
                    avatarImage.getWidth() / (numberAvatars + 1), avatarImage.getHeight() >> 1);
            //item name
            leftShopScreen += itemWidth + paddingLeft - 15;
            int time = Integer.parseInt(gameData.items[i + numberItems].timeLimit) / 24;
            PiPoDesigner.drawString(g, time + " ngày", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, leftShopScreen, topShopScreen + 10);
            // item price
            leftShopScreen += 100;
            PiPoDesigner.drawString(g, "Giá : " + gameData.items[i + numberItems].price, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftShopScreen, topShopScreen + 25);
            topShopScreen += itemHeight + paddingTop;
            fillborder(g, leftScreen, topShopScreen, canvasWidth, 1, 0xedc900);
            if (i == itemSelectedIndex) {
                g.setColor(0xffffff);
                g.fillRect(leftScreen, topShopScreen - itemHeight - 2 * paddingTop - 1, canvasWidth, 1);
                g.fillRect(leftScreen, topShopScreen, canvasWidth, 1);
            }
        }
        paintMyMoney(g);
    }

    private void paintPageMark(Graphics g) {
        if (numberPages < 2) {
            return;
        }
        if (screenMode == SCREEN_PORTRAIT) {
            for (int i = 0; i < numberPages; i++) {
                if (i == currentPageIndex) {
                    Utils.drawFrame(g, dotImages, dotPaddingLeft + (DISTANCE_BETWEEN_DOT + dotImageWidth) * i,
                            canvasHeight - bottomBarHeight - 15,
                            0, 0, dotImageWidth, dotImages.getHeight());
                } else {
                    Utils.drawFrame(g, dotImages, dotPaddingLeft + (DISTANCE_BETWEEN_DOT + dotImageWidth) * i,
                            canvasHeight - bottomBarHeight - 15, 1, 0, dotImageWidth, dotImages.getHeight());
                }
            }
        } else {
            for (int i = 0; i < numberPages; i++) {
                if (i == currentPageIndex) {
                    Utils.drawFrame(g, dotImages, dotPaddingLeft + (DISTANCE_BETWEEN_DOT + dotImageWidth) * i,
                            canvasHeight - 25,
                            0, 0, dotImageWidth, dotImages.getHeight());
                } else {
                    Utils.drawFrame(g, dotImages, dotPaddingLeft + (DISTANCE_BETWEEN_DOT + dotImageWidth) * i,
                            canvasHeight - 25, 1, 0, dotImageWidth, dotImages.getHeight());
                }
            }
        }
    }

    private void paintMyMoney(Graphics g) {
        if (screenMode == SCREEN_PORTRAIT) {
            g.setColor(255, 201, 14);
            g.drawRect(canvasWidth - 70, topBarImage.getHeight() + 2, 60, 20);
            g.setColor(0x000000);
            g.fillRect(canvasWidth - 69, topBarImage.getHeight() + 3, 58, 18);
            PiPoDesigner.drawCenterString(g, String.valueOf(myMoney), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW,
                    canvasWidth - 40, topBarImage.getHeight() + 3);
        } else {
            g.setColor(255, 201, 14);
            g.drawRect(canvasWidth - 70, topScreen + 4, 60, 20);
            g.setColor(0x000000);
            g.fillRect(canvasWidth - 69, topScreen + 5, 58, 18);
            PiPoDesigner.drawCenterString(g, String.valueOf(myMoney), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW,
                    canvasWidth - 40, topScreen + 5);
        }
    }

    private void fillborder(Graphics g, int x, int y, int width, int height, int color) {
        g.setColor(color);
        g.fillRect(x, y, width, height);

    }

    private void paintBagItem(Graphics g) {
        int paddingTop = 7;
        int paddingLeft = 8;
        int topShopScreen = topBarImage.getHeight() + 5;
        if (screenMode == SCREEN_PORTRAIT) {
            PiPoDesigner.drawString(g, "Túi đồ của bạn hiện có", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 5, topShopScreen);
            topShopScreen += 30;
        }
        fillborder(g, leftScreen, topShopScreen, canvasWidth, 1, 0xedc900);
        for (int i = itemBuyStartIndex; i < numberItemsShow + itemBuyStartIndex; i++) {
            int leftShopScreen = 0;
            topShopScreen += 1;
            if (i == itemSelectedIndex) {
                g.drawImage(rowTransparentSelected, leftScreen, topShopScreen, 0);
            } else {
                g.drawImage(rowTransparentBackground, leftScreen, topShopScreen, 0);
            }
            topShopScreen += paddingTop;
            leftShopScreen += paddingLeft;
            ItemBuy itemBuy = gameData.listItemBuy[i];
            //icon
            if (itemBuy.id <= numberItems - 1) {
                Utils.drawFrame(g, shopItems, leftShopScreen, topShopScreen, itemBuy.id, 0,
                        itemWidth, itemHeight);
            } else {
                Utils.drawFrame(g, avatarImage, leftShopScreen, topShopScreen, itemBuy.id - numberItems + 1, myGender,
                        avatarImage.getWidth() / (numberAvatars + 1), avatarImage.getHeight() >> 1);
            }
            //item name
            leftShopScreen += itemWidth + paddingLeft;
            PiPoDesigner.drawString(g, itemBuy.name, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, leftShopScreen, topShopScreen + 10);
            // number items
            leftShopScreen += 30;
            if (itemBuy.isActive == 0) {
                if (itemBuy.id != 3) {
                    PiPoDesigner.drawString(g, "Chưa sử dụng ", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftShopScreen, topShopScreen + 25);
                }
            } else {
                StringBuffer heureRemain = new StringBuffer();
                if (itemBuy.heureRemain < 10) {
                    heureRemain.append(0);
                }
                heureRemain.append(itemBuy.heureRemain);
                StringBuffer minuteRemain = new StringBuffer();
                if (itemBuy.minuteRemain < 10) {
                    minuteRemain.append(0);
                }
                minuteRemain.append(itemBuy.minuteRemain);

                PiPoDesigner.drawString(g, "Hiệu lực  " + heureRemain.toString() + ":" + minuteRemain.toString(), PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftShopScreen, topShopScreen + 25);

            }
            topShopScreen += itemHeight + paddingTop;
            fillborder(g, leftScreen, topShopScreen, canvasWidth, 1, 0xedc900);
            if (i == itemSelectedIndex) {
                g.setColor(0xffffff);
                g.fillRect(leftScreen, topShopScreen - itemHeight - 2 * paddingTop - 1, canvasWidth, 1);
                g.fillRect(leftScreen, topShopScreen, canvasWidth, 1);
            }
        }
        paintMyMoney(g);

    }

    private void paintShopItem(Graphics g) {
        int paddingTop = 7;
        int paddingLeft = 8;
        int topShopScreen = topBarImage.getHeight() + 5;
        if (screenMode == SCREEN_PORTRAIT) {
            if (currentPageIndex == 0) {
                PiPoDesigner.drawString(g, "Vật phẩm", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 5, topShopScreen);
            } else if (currentPageIndex == 1) {
                PiPoDesigner.drawString(g, "Đồ cưới", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 5, topShopScreen);
            } else if (currentPageIndex == 2) {
                PiPoDesigner.drawString(g, "Tiện ích", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, 5, topShopScreen);
            }
            topShopScreen += 30;
        }

        fillborder(g, leftScreen, topShopScreen, canvasWidth, 1, 0xedc900);
        for (int i = itemShopStartIndex; i < numberItemsShow + itemShopStartIndex; i++) {
            int leftShopScreen = 0;
            topShopScreen += 1;
            if (i == itemSelectedIndex) {
                g.drawImage(rowTransparentSelected, leftScreen, topShopScreen, 0);
            } else {
                g.drawImage(rowTransparentBackground, leftScreen, topShopScreen, 0);
            }
            topShopScreen += paddingTop;
            leftShopScreen += paddingLeft;
            // g.drawImage(shopItems[i + itemShopStartIndex], leftShopScreen, topShopScreen, 0);
            Utils.drawFrame(g, shopItems, leftShopScreen, topShopScreen, i, 0,
                    itemWidth, itemHeight);
            //item name
            leftShopScreen += itemWidth + paddingLeft - 15;
            PiPoDesigner.drawString(g, gameData.items[i].name, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, leftShopScreen, topShopScreen + 10);
            // PiPoDesigner.drawString(g, "De thuc hien cau hon va ket hon", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_RED, leftShopScreen, topShopScreen + 30);
            String comment = commentString[i];
            PiPoDesigner.drawString(g, leftShopScreen, topShopScreen + 30, comment, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_RED, 0, comment.length(), 150);
            // item price
            leftShopScreen += 100;
            PiPoDesigner.drawString(g, "Giá : " + gameData.items[i].price, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftShopScreen, topShopScreen + 10);
            topShopScreen += itemHeight + paddingTop;
            fillborder(g, leftScreen, topShopScreen, canvasWidth, 1, 0xedc900);
            if (i == itemSelectedIndex) {
                g.setColor(0xffffff);
                g.fillRect(leftScreen, topShopScreen - itemHeight - 2 * paddingTop - 1, canvasWidth, 1);
                g.fillRect(leftScreen, topShopScreen, canvasWidth, 1);
            }
        }
        paintMyMoney(g);
    }

    protected void paintBarMenu(Graphics g) {
        g.drawImage(bottomBarImage, leftScreen, canvasHeight - bottomBarImage.getHeight(), 0);
        g.drawImage(topBarImage, leftScreen, topScreen, 0);
        PiPoDesigner.drawString(g, "Cửa hàng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, canvasWidth / 2 - 25, 5);
        if (showType == ITEM_SHOP) {
            if (screenMode == SCREEN_PORTRAIT) {
                PiPoDesigner.drawString(g, "Mua", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - PiPoDesigner.stringWidth("mua") / 2, canvasHeight - 25);
            }
        } else if (showType == ITEM_BAG) {
            if (gameData.numberItemBuy > 0) {
                if (gameData.listItemBuy[itemSelectedIndex].isActive == 0 && screenMode == SCREEN_PORTRAIT) {
                    PiPoDesigner.drawString(g, "Sử dụng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - PiPoDesigner.stringWidth("su dung") / 2, canvasHeight - 25);
                }
            }
        }
        super.paintBarMenu(g);
    }
    //-------------------------------------------------------------------------

    public void doKeyLeft() {
        if (numberPages < 2) {
            return;
        }
        if (showType == LIST_AVATAR) {
            itemAvaStartIndex = slidePageLeft(itemAvaStartIndex, numberAvatars);
        } else if (showType == ITEM_BAG) {
            itemBuyStartIndex = slidePageLeft(itemBuyStartIndex, gameData.numberItemBuy);
        } else {
            itemShopStartIndex = slidePageLeft(itemShopStartIndex, numberItems);
        }
        parent.repaint();
    }

    private int slidePageLeft(int startIndex, int number) {
        Debug.d("start index  :" + numberPages);
        Debug.d("start index before :" + startIndex);
        startIndex = ((startIndex -= NUMBER_ICON_PER_PAGE) < 0 ? (numberPages - 1) * NUMBER_ICON_PER_PAGE : startIndex);
        Debug.d("start index :" + startIndex);
        if (number - startIndex < NUMBER_ICON_PER_PAGE) {
            numberItemsShow = number - startIndex;
        } else {
            numberItemsShow = NUMBER_ICON_PER_PAGE;
        }
        itemSelectedIndex = startIndex;
        currentPageIndex = (--currentPageIndex < 0 ? (numberPages - 1) : currentPageIndex);
        return startIndex;
    }

    private int slidePageRight(int startIndex, int number) {
        startIndex += NUMBER_ICON_PER_PAGE;
        if (startIndex >= number) {
            startIndex = 0;
        }
        if (number - startIndex < NUMBER_ICON_PER_PAGE) {
            numberItemsShow = number - startIndex;
        } else {
            numberItemsShow = NUMBER_ICON_PER_PAGE;
        }
        itemSelectedIndex = startIndex;
        currentPageIndex = (++currentPageIndex) % numberPages;
        return startIndex;
    }

    public void doKeyRight() {
        if (numberPages < 2) {
            return;
        }
        if (showType == LIST_AVATAR) {
            itemAvaStartIndex = slidePageRight(itemAvaStartIndex, numberAvatars);
        } else if (showType == ITEM_BAG) {
            itemBuyStartIndex = slidePageRight(itemBuyStartIndex, gameData.numberItemBuy);
        } else {
            itemShopStartIndex = slidePageRight(itemShopStartIndex, numberItems);
        }
        parent.repaint();
    }

    public void doKeyPound() {
    }

    protected void doKeyUpRemain() {
        int startIndex = 0;
        if (showType == ITEM_BAG) {
            startIndex = itemBuyStartIndex;
        } else if (showType == ITEM_SHOP) {
            startIndex = itemShopStartIndex;
        } else {
            startIndex = itemAvaStartIndex;
        }
        itemSelectedIndex -= 1;
        if (itemSelectedIndex < startIndex) {
            itemSelectedIndex += numberItemsShow;
        }
        parent.repaint();
    }

    protected void doKeyDownRemain() {
        int startIndex = 0;
        if (showType == ITEM_BAG) {
            startIndex = itemBuyStartIndex;
        } else if (showType == ITEM_SHOP) {
            startIndex = itemShopStartIndex;
        } else {
            startIndex = itemAvaStartIndex;
        }
        itemSelectedIndex += 1;
        if (itemSelectedIndex > startIndex + numberItemsShow - 1) {
            itemSelectedIndex = startIndex;
        }
        parent.repaint();

    }

    protected void doSoftKeyLeftRemain() {
    }

    protected void doSoftKeyRightRemain() {
        showChooseGame();
    }

    protected void reponseQuestion(int result) {
        if (result == 1) {
            if (showType == ITEM_BAG) {
                Request.useItem(itemSelectedIndex);
                return;
            }
            if (showType == ITEM_SHOP) {
                Request.buyItem(itemSelectedIndex);
                return;
            }
            if (showType == LIST_AVATAR) {
                Request.buyItem(itemSelectedIndex + numberItems);
                return;
            }
        }
    }

    protected void doKeyCenterRemain() {
        if (showType == ITEM_BAG) {
            if (gameData.listItemBuy[itemSelectedIndex].isActive == 0) {
                questionString = "Bạn đồng ý sử dụng item này ?";
                parent.repaint();
            }
            return;
        }
        if (showType == ITEM_SHOP) {
            questionString = "Bạn đồng ý mua " + gameData.items[itemSelectedIndex].name;
            questionStringInfo = "";
            parent.repaint();
            return;
        }
        if (showType == LIST_AVATAR) {
            questionString = "Bạn đồng ý mua " + gameData.items[itemSelectedIndex + numberItems].name;
            questionStringInfo = "";
            parent.repaint();
        }

    }

    public void inputKeyCode(int key) {
    }

    protected void touchPressedRemain(int x, int y) {
    }

    public void doTouchReleased(int x, int y) {
    }

    public void doTouchDraged(int x, int y) {
    }

    public void processMessage(String[] part) {
        super.processMessage(part);
        if (part[0].equals("BUY")) {
            int result = Integer.parseInt(part[1]);
            if (result == 0) {
                dialogString = "Bạn không đủ tiền mua item này";
                parent.repaint();
            } else {
                dialogString = "Bạn đã mua item thành công";
                parent.repaint();
            }
        }
        if (part[0].equals("CM")) {
            int deltaMoney = Integer.parseInt(part[1]);
            myMoney = myMoney + deltaMoney;
            gameData.setMyMoney(myMoney);
            parent.repaint();
        }
        if (part[0].equals("MYI")) {
            gameData.initShopData(part);
            parent.repaint();
        }
    }

    private void setUpScene(int index) {
        numberPages = 0;
        if (index == 0) {
            showType = LIST_AVATAR;
            numberPages = numberAvatars / NUMBER_ICON_PER_PAGE;
            if (numberAvatars % NUMBER_ICON_PER_PAGE != 0) {
                numberPages += 1;
            }
        } else if (index == 1) {
            showType = ITEM_SHOP;
            numberPages = numberItems / NUMBER_ICON_PER_PAGE;
            if (numberItems % NUMBER_ICON_PER_PAGE != 0) {
                numberPages += 1;
            }
        } else if (index == 2) {
            showType = ITEM_BAG;
            numberPages = gameData.numberItemBuy / NUMBER_ICON_PER_PAGE;
            if (gameData.numberItemBuy % NUMBER_ICON_PER_PAGE != 0) {
                numberPages += 1;
            }
        }
        itemAvaStartIndex = 0;
        itemBuyStartIndex = 0;
        itemShopStartIndex = 0;
        itemSelectedIndex = 0;
        numberItemsShow = NUMBER_ICON_PER_PAGE;
        if (showType == ITEM_BAG && numberItemsShow > gameData.numberItemBuy) {
            numberItemsShow = gameData.numberItemBuy;
        }
        currentPageIndex = 0;
        Debug.d("number page :" + numberPages);
        if (screenMode == SCREEN_PORTRAIT) {
            dotPaddingLeft = (canvasWidth - (numberPages - 1) * (DISTANCE_BETWEEN_DOT + dotImageWidth)) >> 1;
        } else {
            dotPaddingLeft = canvasWidth / 2 - 50;
        }
    }

    protected void doSubMenu() {
        setUpScene(subMenu.getSelectedIndex());
        isShowSubMenu = false;
        subMenu.setVisible(false);
        parent.repaint();
    }
    //----------------------------------------------------------------------

    public void dispose() {
        bottomBarImage = null;
        topBarImage = null;
        shopItems = null;
        rowTransparentBackground = null;
        dotImages = null;
        rowTransparentSelected = null;
        gameData.diposeItem();
        super.dispose();
    }
}
